package com.appology.oauth2;

import com.google.api.client.extensions.appengine.auth.AbstractAppEngineFlowServlet;
import com.google.api.client.extensions.auth.helpers.Credential;
import com.google.api.client.extensions.auth.helpers.ThreeLeggedFlow;
import com.google.api.client.googleapis.extensions.auth.helpers.oauth2.draft10.GoogleOAuth2ThreeLeggedFlow;
import com.google.api.services.buzz.Buzz;
import com.google.api.services.buzz.Buzz.People;
import com.google.api.services.buzz.Buzz.People.Get;
import com.google.api.services.buzz.model.Person;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import java.io.IOException;

import javax.jdo.PersistenceManagerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appology.users.ChuckUser;

/**
 * This is the main entry point for the example. It uses the
 * {@link AbstractFlowUserServlet} to require an authorized session for the
 * current user prior to invoking the
 * {@link #doGetWithCredentials(HttpServletRequest, HttpServletResponse, Credential)}
 * method.
 * 
 */

public class Auth extends AbstractAppEngineFlowServlet {

	private static final long serialVersionUID = 1L;
	private static final String ME = "@me";
	private static final String DESTINATION = "/index.jsp";

	private static String id = null;
	private static Buzz buzz;
	private static Person person;

	@Override
	protected ThreeLeggedFlow newFlow(String userId) {

		return new GoogleOAuth2ThreeLeggedFlow(userId,
				OAuth2ClientCredentials.CLIENT_ID,
				OAuth2ClientCredentials.CLIENT_SECRET,
				OAuth2ClientCredentials.SCOPE,
				OAuth2ClientCredentials.CALLBACK_URL);
	}

	/**
	 * This method attempts to get the user's display name and Buzz id using the
	 * Buzz api. This method is only called if the user is logged in and has
	 * granted permission to access their Buzz account.
	 * 
	 * @see com.google.api.client.extensions.servlet.auth.AbstractFlowUserServlet#doGetWithCredentials(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      com.google.api.client.extensions.auth.helpers.Credential)
	 */
	@Override
	protected void doGetWithCredentials(HttpServletRequest req,
			HttpServletResponse resp, Credential credential) throws IOException {

		// Get an instance of the UserServiceFactory so we can get the current
		// user.
		UserService userService = UserServiceFactory.getUserService();

		// If there is a user logged in (which there should be)
		// get their Google id.
		if (userService.getCurrentUser() != null) {
			id = userService.getCurrentUser().getUserId();
		}

		// Instantiate a new Buzz object using the transport, and jsonFactory
		// provided by the superclass
		// and the credential obtained from the OAuth.
		buzz = new Buzz(getHttpTransport(), credential, getJsonFactory());

		// Get the Person for the currently logged in Buzz user.
		person = buzz.people.get(ME).execute();

		// Create a new ChuckUser with the User's id and the
		// Person's Buzz id and display name
		ChuckUser chuckUser = new ChuckUser(id, (String) person.id,
				(String) person.displayName);

		// Save the ChuckUser to the datastore
		chuckUser.save();

		// Redirect the user back to the landing page, but now they will be
		// logged in.

		resp.sendRedirect(resp.encodeRedirectURL(DESTINATION));

	}

	@Override
	protected PersistenceManagerFactory getPersistenceManagerFactory() {
		return PMF.get();
	}
}